<template>
  <div class="app-container">
    <SearchContent :queryParams="queryParams" @query="handleQuery" @reset="resetQuery">
      <el-col :span="6">
        <el-form-item label="座位数:" prop="seatCount">
          <el-input v-model="queryParams.seatCount" clearable placeholder="请输入座位数" @input="queryParams.seatCount=queryParams.seatCount.replace(/^0|[^0-9]/g, '')"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="城市:" prop="cityId">
          <el-select v-model="queryParams.cityId" filterable clearable>
            <template v-for="item in cityList">
              <el-option :label="item.regionName" :value="item.regionId" :key="item.regionId"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
    </SearchContent>

    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end" v-hasPermi="['channel:yearFee:add']">
        <el-button type="primary" size="mini" @click="handleAdd">新增</el-button>
      </div>

      <table-page :loading="loading" :dataList="dataList" border :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @setSizeCurrent="getList">
        <!-- 操作插槽 -->
        <template slot="tableColAc">
          <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <e-button v-hasPermi="['channel:yearFee:edit']" size="mini" type="text" @click="handleUpdate(scope.row)">编辑
              </e-button>
              <el-button v-hasPermi="['channel:yearFee:void']" type="text" @click="voids(scope.row)">作废</el-button>
            </template>
          </el-table-column>
        </template>
        <!-- 普通列插槽 -->
        <template slot="tableCol">
          <el-table-column label="最小座位数" align="center" prop="startSeatCount" />
          <el-table-column label="最大座位数(含)" align="center" prop="endSeatCount" />
          <el-table-column label="城市" align="center" prop="cityName" />
          <el-table-column label="年检费(元)" align="center" prop="annualInspectionFee">
            <template slot-scope="scope">
              <span>{{scope.row.annualInspectionFee}}</span>
            </template>
          </el-table-column>
        </template>
      </table-page>
    </div>

    <!-- 添加或修改测试单表对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body class="form-dialog" width="40%">
      <div style="width: 70%; margin: 0 auto;">
        <el-form ref="form" :model="form" label-width="150px" :rules="rules">
          <el-form-item label="最小座位数:" prop="startSeatCount">
            <el-input v-model="form.startSeatCount" maxlength="5" @input="form.startSeatCount=form.startSeatCount.replace(/^0|[^0-9]/g, '')" style="width: 100%;"></el-input>
          </el-form-item>
          <el-form-item label="最大座位数(含):" prop="endSeatCount">
            <el-input v-model="form.endSeatCount" maxlength="5" @input="form.endSeatCount=form.endSeatCount.replace(/^0|[^0-9]/g, '')" style="width: 100%;"></el-input>
          </el-form-item>
          <el-form-item label="城市:" prop="cityId">
            <el-select v-model="form.cityId" filterable clearable style="width: 100%;">
              <template v-for="item in cityList">
                <el-option :label="item.regionName" :value="item.regionId" :key="item.regionId"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="年检费(元):" prop="annualInspectionFee">
            <el-input v-model="form.annualInspectionFee" maxlength="13" clearable @input="form.annualInspectionFee=form.annualInspectionFee.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="cancel">取消</el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

  <script>
import SearchContent from "@/components/SearchContent/index.vue";
import {
  getYearFeeList,
  yearFeeAdd,
  yearFeeUpdate,
  yearFeeDelete,
  yearFeeLion,
} from "@/api/globalField";
import { regionList } from "@/api/system/dept.js";

export default {
  name: 'YearFee',
  components: {
    SearchContent,
  },
  dicts: ["business_scope"],
  data() {
    return {
      cityList: [],
      rules: {
        startSeatCount: [
          { required: true, message: "请输入最小座位数:", trigger: "change" },
        ],
        endSeatCount: [
          { required: true, message: "请输入最大座位数", trigger: "change" },
        ],
        cityId: [{ required: true, message: "请选择城市", trigger: "change" }],
        annualInspectionFee: [
          { required: true, message: "请输入年检费用", trigger: "change" },
        ],
      },
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      isNew: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dataList: [],
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getCity();
  },
  methods: {
    getCity() {
      regionList({
        regionLevel: 2, //显示级别 1省 2省市 3省市区 ；为空默认3
        areaId: null, //区域id；为空默认全国
      }).then((res) => {
        this.cityList = res.data.filter((item) => {
          return ![2].includes(item.regionLevel);
        });
      });
    },
    voids(row) {
      this.$confirm("确认作废?", "作废", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          yearFeeDelete(row.id).then((res) => {
            this.$message({
              type: "success",
              message: "作废成功",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消作废",
          });
        });
    },
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      getYearFeeList(this.queryParams).then((res) => {
        if (res.code == "200") {
          const { rows, total } = res;
          this.dataList = rows;
          this.total = total;
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    reset() {
      this.form = {
        includeStart: 0,
        includeEnd: 1,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      }),
        this.handleQuery();
    },
    /** 新增操作 */
    handleAdd() {
      this.open = true;
      this.title = "新增";
      this.$refs["form"] && this.$refs["form"].resetFields();
      this.reset();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.open = true;
      this.title = "编辑";
      yearFeeLion(row.id).then((res) => {
        this.form = res.data;
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (Number(this.form.endSeatCount) >= Number(this.form.startSeatCount)) {
            if (this.title == "新增") {
              yearFeeAdd(this.form).then((res) => {
                this.open = false;
                this.getList();
                this.$message.success("新增成功");
              });
            } else {
              // 调取接口
              yearFeeUpdate(this.form).then((res) => {
                this.open = false;
                this.getList();
                this.$message.success("编辑成功");
              });
            }
          } else {
            this.$message.error("最大座位数必须大于或等于最小座位数,请重新输入");
            this.form.endSeatCount = null;
            return false;
          }
        }
      });
    },
  },
};
</script>


  <style lang="scss" scoped>
.pagination-container {
  padding: 0 20px 50px !important;
}

.form-dialog {
  ::v-deep .el-dialog__header {
    padding: 10px 20px;
    border-bottom: 1px solid #dcdfe6;

    .el-dialog__headerbtn {
      top: 15px;
    }
  }

  .box-card {
    margin-bottom: 20px;

    .card-title {
      font-size: 16px;
      position: relative;
      margin-left: 8px;
      padding: 0;
    }

    .card-title::after {
      content: "";
      width: 4px;
      height: 18px;
      background-color: aqua;
      display: block;
      position: absolute;
      left: -12px;
      top: 2px;
    }
  }
}
</style>
